﻿using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace TrafficCountingTool.ViewModel
{
    public delegate void ViewModelMessageReceivedEventHandler(object sender, object message);

    public class ViewModel : INotifyPropertyChanged
    {
        public bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (object.Equals(storage, value)) return false;

            storage = value;
            OnPropertyChange(propertyName);
            return true;
        }

        protected void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void SendMessage(object message)
        {
            if (ViewModelMessageReceived != null)
            {
                ViewModelMessageReceived(this, message);
            }
        }

        public event ViewModelMessageReceivedEventHandler ViewModelMessageReceived;
    }
}